package com.emar.adx.yiqifa.thread;

import com.emar.adx.yiqifa.util.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

/**
 * Created by xiaoliang
 * 2016.12.05 13:46
 *
 * @Version 1.0
 */
public class RunMainThread {
    private static final Logger logger = LoggerFactory.getLogger(RunMainThread.class);

    public static void main(String[] args) {
        int taskNums = 10;
        int  total = 500;
        String type = "1";
        String table = "";

        if (args.length < 2) {
            System.err.println("Usage: Please specify 4 params : taskNums total type table");
            System.err.println("taskNums : thread nums ");
            System.err.println("execute nums : every thread execute nums ");
            System.err.println("type : 1 batch ,0 single");
            System.err.println("table : tableinfo txt");
            ClassLoader classLoader = RunMainThread.class.getClassLoader();
            String datafile = classLoader.getResource("tables.txt").getFile();
            table = datafile;
        }else {
            System.err.println("Usage: Please specify 4 params : taskNums total type table");
            System.err.println("taskNums : thread nums ");
            System.err.println("execute nums : every thread execute nums ");
            System.err.println("type : 1 batch ,0 single");
            System.err.println("table : tableinfo txt");
            taskNums = Integer.parseInt(args[0].toString());
            total =   Integer.parseInt(args[1].toString());
            type = args[2].toString();
            table = args[3].toString();
            System.exit(0);
        }

        Map<Integer,String> tableMap = new HashMap<Integer, String>();
        int index = 0;

        try {
            InputStream in = FileUtils.getFileInputStream(table);
            Reader reader = new InputStreamReader(in);
            BufferedReader br = new BufferedReader(reader);
            try {
                String line = br.readLine();
                while(line != null){
                    tableMap.put(index,line);
                    index ++;
                    line = br.readLine();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        ExecutorService fixedThreadPool = Executors.newFixedThreadPool(taskNums);

        long start = System.currentTimeMillis();
        logger.info("thread start job : " + start );


        if("1".equalsIgnoreCase(type)){
            for(int i = 0;i<taskNums;i++){
                OracleDBBatchThread thread = new OracleDBBatchThread(tableMap,index);
                thread.setTotal(total);
                fixedThreadPool.execute(thread);
            }
        }else {
            for(int i = 0;i<taskNums;i++){
                OracleDBThread thread = new OracleDBThread(tableMap,index);
                thread.setTotal(total);
                fixedThreadPool.execute(thread);
            }
        }

        fixedThreadPool.shutdown();
        try {
            while (!fixedThreadPool.awaitTermination(1, TimeUnit.SECONDS)) {

            }
        } catch (InterruptedException e) {

            logger.error(" InterruptedException ",e);
        }

        long end = System.currentTimeMillis();
        logger.info(" end job : " + (end-start)/1000 + "s ...");
    }
}
